package cn.edu.neusoft.yuejian4.yuejian_4.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yuejian4.yuejian_4.Config.Server;
import cn.edu.neusoft.yuejian4.yuejian_4.R;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.ClubDetailActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.activity.ShowDetailsActivity;
import cn.edu.neusoft.yuejian4.yuejian_4.javaBean.Slideshow;

/**
 * Created by Lix on 2018/10/15.
 */

public class ShowListAdapter extends RecyclerView.Adapter<ShowListAdapter.ViewHolder> {

    private List<Slideshow> mSlideshow;
    private Context mContext;
    mOnItemClickListener mOnItemClickListener;

    public ShowListAdapter(Context mContext, List<Slideshow> mSlideshow){
        this.mContext = mContext;
        this.mSlideshow = mSlideshow;
        //mLayoutInflater = LayoutInflater.from(mContext);
    }



    @Override
    public ShowListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_cardview,parent,false);
        ShowListAdapter.ViewHolder holder = new ShowListAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ShowListAdapter.ViewHolder holder, final int position) {

        final Slideshow slideshow = mSlideshow.get(position);
        String showAllPic = slideshow.getPic();
        String[] Str1Array = showAllPic.split("#");//结果Str1Array：[**.jpg, **.jpg, **.jpg]
        Picasso.with(mContext)
                .load(Server.PIC_URL_J+Str1Array[0])
                .placeholder(R.drawable.ic_default_img)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .into(holder.show_img);
        Picasso.with(mContext)
                .load(Server.PIC_URL_J+slideshow.getUser_pic())
                .placeholder(R.drawable.ic_default_img)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .into(holder.user_img);

        holder.user_name.setText(slideshow.getNickname());
        holder.show_time.setText(slideshow.getOpentime());
        holder.show_intro.setText(slideshow.getIntro());
        holder.show_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ShowDetailsActivity.class);
                intent.putExtra("show_id",slideshow.getId());
                mContext.startActivity(intent);
            }
        });


        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener!=null) {
                    mOnItemClickListener.onClick(v, position);
                }
            }
        };
        holder.delete_img.setVisibility(View.VISIBLE);
        holder.delete_img.setOnClickListener(onClickListener);


    }

    @Override
    public int getItemCount() {
        return mSlideshow.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView user_img,show_img,delete_img;
        TextView user_name,show_intro,show_time;
        FrameLayout show_container;
        LinearLayout show_com;

        public ViewHolder(View view){
            super(view);
            user_img= (ImageView)view.findViewById(R.id.show_user_img);
            show_img= (ImageView)view.findViewById(R.id.show_img);
            user_name = (TextView)view.findViewById(R.id.show_user_name);
            show_intro = (TextView)view.findViewById(R.id.show_intro);
            show_time = (TextView)view.findViewById(R.id.show_time);
            show_container = (FrameLayout)view.findViewById(R.id.show_container);
            delete_img = (ImageView)view.findViewById(R.id.delete_my_show);
            show_com = (LinearLayout)view.findViewById(R.id.show_cont);
        }
    }

    public void setImageGone(ImageView img){
        img.setVisibility(View.VISIBLE);
    }

    public interface mOnItemClickListener {
        void onClick(View view, int position);
    }

    public void setmOnItemClickListener(ShowListAdapter.mOnItemClickListener mOnItemClickListener) {
        this.mOnItemClickListener = mOnItemClickListener;
    }
}
